\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,xparse,calc}
\RequirePackage{xifthen}
\GetIdInfo$Id: postgraduate.dtx v1.0.0(testing)  2019-10-08  ZhenhuaFeng  <fengzhenhua@sina.cn> $ {For Chinese Postgraduate Entrance Examination}
\ProvidesExplPackage{\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
%总页码计数器
\newcounter{cex@mpagew}%
\newcounter{cex@mpage}%
\newcounter{cex@mans}%
\newcounter{cex@mctrl}%
% 如果不存在章的类中使用此宏包，则定义章计数器。这样做能够减少在后面引用总页码时的代码，因为不用分别判断是否存在章命令了。
\expandafter\ifx\csname chapter\endcsname\relax
   \newcounter{chapter}
\else
   \relax
\fi
%定义生成页码命令%
\DeclareRobustCommand\MakePage[2]{%
   \expandafter\ifx\csname cex@mpage#1\endcsname\relax
      \expandafter\def\csname cex@mpage#1\endcsname{#2}%
   \else
      \setcounter{cex@mctrl}{\csname cex@mpage#1\endcsname} 
    \ifthenelse{\cnttest{\thecex@mctrl}{<}{#2}}{%
	 \expandafter\def\csname cex@mpage#1\endcsname{#2}%
      }{%
	 \relax
      }%
   \fi
}%
%定义总页码
\def\cex@mpagetotal {%
   \expandafter\ifx\csname cex@mpage\the\c@chapter\the\c@section\thecex@mans\endcsname\relax
      \ ??
   \else
      \csname cex@mpage\the\c@chapter\the\c@section\thecex@mans\endcsname
   \fi
}%
% 定义总页码写出
\newwrite\@Page
%插入总页码文件
\openout\@Page=\jobname.page
\IfFileExists{\jobname.page}{%
   \input{\jobname.page}%
}{}%
% 定义页脚格式
\def\ns@oddfoot{%
   \parbox{\linewidth}{
      \rule[5pt]{\linewidth}{.4pt}
      \newline
      科目名称：\cex@mclass
      \hfill 第\thecex@mpage{}页 \quad{}共\cex@mpagetotal{}页
      \write\@Page{\MakePage{\the\c@chapter\the\c@section\thecex@mans}{\thecex@mpagew}}%
   }%
   \setcounter{cex@mpagew}{\thecex@mpage}%
   \stepcounter{cex@mpage}%
}
\def\ns@oddhead{\hfill\thepage}
\def\ns@evenhead{\thepage\hfill}
\def\ps@exam{%
%  \let\@mkboth\@gobbletwo
%  \let\@oddhead\@empty%
   \let\@oddhead\ns@oddhead%
  \let\@oddfoot\ns@oddfoot%
%  \let\@evenhead\@oddhead%
  \let\@evenhead\ns@evenhead%
  \let\@evenfoot\@oddfoot%
}
% 定义学校和科目1 学校 ，2 科目
\def\cex@mschool {school}
\def\cex@mclass {class}
\def\cex@myear {1949}
\def\cex@mscore{150}
\def\cex@mtime{180}
\newboolean{cex@mpostgraduate}
\DeclareRobustCommand\postgraduate[2]{%
   \newpage
   \pagestyle{exam}
   \setboolean{cex@mpostgraduate}{true}
   \def\cex@mschool{#1}
   \def\cex@mclass{#2}
   \expandafter\ifx\csname chapter\endcsname\relax
      \addcontentsline{toc}{section}{#1}{}
   \else
      \addcontentsline{toc}{chapter}{#1}{}
    \fi
   \cexam_answer_add:n \postgraduate{#1}{#2（答案）}\scan_stop:
}
\DeclareRobustCommand\postschool[1]{
   \def\cex@mschool{#1}
   \cexam_answer_add:n \postschool{#1}\scan_stop:
}
\DeclareRobustCommand\postclass[1]{
   \def\cex@mclass{#1}
   \cexam_answer_add:n \postclass{#1（答案）}\scan_stop:
}
\newcounter{cex@mnotice}
\DeclareRobustCommand\setpostnotice[1]{\setcounter{cex@mnotice}{#1}}
\DeclareRobustCommand\postnotice[1]{
   \stepcounter{cex@mnotice}
   \expandafter\def\csname postnotice\thecex@mnotice\endcsname
   {#1}
   \cexam_answer_add:n \setpostnotice{\thecex@mnotice}\scan_stop:
}
\DeclareRobustCommand\postyear[1]
{%
   \ifthenelse{\boolean{cex@mpostgraduate}}{
      \setboolean{cex@mpostgraduate}{false}
   }{
      \newpage
   }
   \setcounter{cex@mpage}{1}
   \stepcounter{cex@mans}
   {\parindent=0pt
      \begin{center}
	 \large\bf%
	 \cex@mschool\\[.4\ccwd]
	 {#1}年招收硕士研学位研究生入学统一考试试题\\[.4\ccwd]
	 科目名称：\cex@mclass
      \end{center}
      考生须知：\csname postnotice\thecex@mnotice\endcsname
      \rule[5pt]{\linewidth}{.4pt}
      \expandafter\ifx\csname chapter\endcsname\relax
	 \addcontentsline{toc}{subsection}{#1年\cex@mclass}{}
      \else
	 \addcontentsline{toc}{section}{#1年\cex@mclass}{}
      \fi
   }
   \cexam_answer_add:n \postyear{#1}\scan_stop:
}
%% 暂时追加一部分题目输入格式定义 
\newcounter{cex@mproblem}
\cs_new:Nn \cexam_problem: {TotalNumber}
\cs_new:Nn \cexam_score: {TotalScore}
\cs_new:Nn \cexam_score_i: {EveryScore}
\dim_new:N \cexam_problem_dim
\dim_new:N \cexam_pb_indent_dim
\cs_new:Nn \cexam_problem_i: 
{
   {\bf \chinese{cex@mproblem}、选择题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。请从题目所列出的选项中选择一正确的项填充空格。每题的四个备选项中只有一个是正确的，不选、错选或多选均不得分。请将你的选择标清题号写在考场发的答题纸上，直接填写在试题上无效。）
}
\cs_new:Nn \cexam_problem_ii:
{
{\bf \chinese{cex@mproblem}、填空题}（本题共\cexam_problem:{}小题,每空\cexam_score_i:{}分，共\cexam_score:{}分。）
}
\cs_new:Nn \cexam_problem_iii:
{
   {\bf \chinese{cex@mproblem}、判断题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。正确：$\times$ ,错误：$\surd$）
}
\cs_new:Nn \cexam_problem_iv:
{
{\bf \chinese{cex@mproblem}、计算题}（本题共\cexam_problem:{}小题,每小题\cexam_score_i:{}分，共\cexam_score:{}分。）
}
\NewDocumentCommand \question{ o o o m}
{
   \stepcounter{cex@mproblem}
   \dim_set:Nn \cexam_pb_indent_dim {1.8\ccwd}
   \dim_set:Nn \cexam_problem_dim {\linewidth}
   \dim_sub:Nn \cexam_problem_dim {\cexam_pb_indent_dim}
   \cs_set:Nn \cexam_problem: {#1}
   \cs_set:Nn \cexam_score: {#2}
   \cs_set:Nn \cexam_score_i: {#3}
   \parindent=0pt
   \parshape=~2~0pt~\linewidth~\cexam_pb_indent_dim~\cexam_problem_dim
   \str_if_in:nnTF {#4}{choice}
   {\cexam_problem_i:}
   {
      \str_if_in:nnTF {#4}{blank}
      {\cexam_problem_ii:}
      {
	 \str_if_in:nnTF {#4}{judgement}
	 {\cexam_problem_iii:}
	 {
	    \str_if_in:nnTF {#4}{calculation}
	    {\cexam_problem_iv:}
	    {{\bf \chinese{cex@mproblem}、}#4}
	 }
      }
   }
}
%%
%% End of file \postgraduate.sty'.
